package com.fpt.edu.vn.h2tv.api.staff;

import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.OPTIONS;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import com.fpt.edu.vn.h2tv.service.FeedbackService;
import com.fpt.edu.vn.h2tv.service.IBaseService;

@Path("/manageFeedback")
public class FeedbackAdminAPI {

    @GET
    @Produces(javax.ws.rs.core.MediaType.APPLICATION_XML)
    public Response manageFeedback() {
        IBaseService service = new FeedbackService();
        return buildResponse(MediaType.APPLICATION_XML, service.getResponse());
    }

    @Path("/filterReaded")
    @GET
    @Produces(javax.ws.rs.core.MediaType.APPLICATION_XML)
    public Response filterReaded() {

        return buildResponse(MediaType.APPLICATION_XML, FeedbackService.filterReaded());
    }

    @Path("/filterUnReaded")
    @GET
    @Produces(javax.ws.rs.core.MediaType.APPLICATION_XML)
    public Response filterUnReaded() {

        return buildResponse(MediaType.APPLICATION_XML, FeedbackService.filterUnReaded());
    }

    @Path("/searchFeedback")
    @GET
    @Produces(javax.ws.rs.core.MediaType.APPLICATION_XML)
    public Response searchFeedback(@QueryParam(value = "txtFeedbackEmail") String email) {

        return buildResponse(MediaType.APPLICATION_XML, FeedbackService.searchFeedback(email));
    }
    
    @Path("/detailFeedback")
    @GET
    @Produces(javax.ws.rs.core.MediaType.APPLICATION_XML)
    public Response detailFeedback(@QueryParam(value = "txtFeedbackId") int id) {

        return buildResponse(MediaType.APPLICATION_XML, FeedbackService.getFeedbackById(id));
    }

    @DELETE
    @Produces(javax.ws.rs.core.MediaType.TEXT_HTML)
    public Response deleteFeedback(@QueryParam(value = "txtFeedbackId") int id) {

        return buildResponse(MediaType.TEXT_HTML, FeedbackService.deleteFeedback(id));
    }

    @PUT
    @Produces(javax.ws.rs.core.MediaType.TEXT_HTML)
    public Response updateFeedback(@QueryParam(value = "txtUpdateFeedback") int id) {

        return buildResponse(MediaType.TEXT_HTML, FeedbackService.updateFeedback(id));
    }

    @OPTIONS
    @Produces(javax.ws.rs.core.MediaType.TEXT_HTML)
    public Response test() {

        return buildResponse(MediaType.TEXT_HTML, "ok");
    }

    private Response buildResponse(String mediaType, Object repsonse) {
        return Response.ok(repsonse, mediaType)
        // .header("Access-Control-Allow-Origin", "http://localhost")
        // .header("Access-Control-Allow-Methods", "GET,POST,PUT,DELETE,OPTIONS,TRACE,HEAD,CONNECT")
        // .header("Access-Control-Allow-Credentials",true)
        // .header("Access-Control-Allow-Headers",
        // "x-accept-charset, x-accept, origin,X-Accept-Charset,X-Accept,Content-Type,Authorization,REFERER")
                .build();
    }

}
